package com.caiyj.observe.push;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @author:CAIYJ
 * @Date:2018/12/14 20:34
 */
public class ConcreteSubject implements Subject {

    String mess;

    boolean change;

    List<Observer> observers;

    public ConcreteSubject() {
        this.observers = new ArrayList<>();
    }

    @Override
    public void addObserver(Observer observer) {
        if (!observers.contains(observer)) {
            observers.add(observer);
        }
    }

    @Override
    public void deleteObserver(Observer observer) {
        if (observers.contains(observer)) {
            observers.remove(observer);
        }
    }

    @Override
    public void notifyObserver() {
        if (change) {
            for (Observer observer : observers) {
                observer.hearTel(mess);
            }
            change = false;
        }
    }

    public void giveNewMessage(String str) {
        if (str.equals(mess)) {
            change = false;
        } else {
            mess = str;
            change = true;
        }
    }

}
